<?php

namespace app\controller;

use app\consts\ExcelConst;
use app\Request;
use app\service\DevelopmentService;
use app\utils\DataProcessor;
use app\utils\Excel;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;

class DevelopmentController
{

    public function getList(Request $request)
    {
        $name = $request->post("name");
        $page = $request->post("page");
        $pageSize = $request->post("pageSize");

        $result = DevelopmentService::getInstance()->getDevelopmentList($name, $page, $pageSize);
        return jsonResponse($result);
    }
    public function create(Request $request)
    {
        validate([
            "name" => [
                "require",
                "unique" => function ($value) {
                    return !Db::name('development')
                        ->where("name", $value)
                        ->where("is_delete", 0)
                        ->find();
                }
            ],
            "sex" => "require|in:1,2",
            "real_name" => "require",
            "position" => "require",
        ],[
            "name.require" => "请输入姓名",
            "name.unique" => "该姓名已存在",
            "sex.require" => "请选择性别",
            "sex.in" => "请选择性别",
            "real_name.require" => "请输入真实姓名",
            "position.require" => "请输入职位",
        ])->check($request->post());
        $name = $request->post("name");
        $sex = $request->post("sex");
        $realName = $request->post("real_name");
        $position = $request->post("position");
        $status = $request->post("status");
        $description = $request->post("description");

        $result = DevelopmentService::getInstance()->createDevelopment($name, $sex, $realName, $position, $status, $description);
        return jsonResponse($result);
    }

    public function update(Request $request)
    {
        validate([
            "id" => "require",
            "name" => "require|unique:development",
            "sex" => "require|in:1,2",
            "real_name" => "require",
            "position" => "require"
        ])->check($request->post());

        $id = $request->post("id");
        $name = $request->post("name");
        $sex = $request->post("sex");
        $realName = $request->post("real_name");
        $position = $request->post("position");
        $status = $request->post("status");
        $description = $request->post("description");

        $result = DevelopmentService::getInstance()->updateDevelopment($id, $name, $sex, $realName, $position, $status, $description);
        return jsonResponse($result);
    }

    public function delete(Request $request)
    {
        validate([
            "id" => "require"
        ])->check($request->post());

        $id = $request->post("id");

        $result = DevelopmentService::getInstance()->deleteDevelopment($id);
        return jsonResponse($result);
    }

    public function batchCreate(Request $request)
    {
        validate([
            "batch_data" => "require"
        ])->check($request->post());
        $batchData = $request->post("batch_data");
        $result = DevelopmentService::getInstance()->batchCreateDevelopment($batchData);
        return jsonResponse($result);
    }

    public function batchDelete(Request $request)
    {
        validate([
            "ids" => "require"
        ])->check($request->post());
        $ids = $request->post("ids");
        $result = DevelopmentService::getInstance()->batchDeleteDevelopment($ids);
        return jsonResponse($result);
    }

    public function batchUpdate(Request $request)
    {
        validate([
            "batch_data" => "require"
        ])->check($request->post());
        $batchData = $request->post("batch_data");
        $result = DevelopmentService::getInstance()->batchUpdateDevelopment($batchData);
        return jsonResponse($result);
    }

    public function import(Request $request)
    {
        $file = $request->file("file");
        $list = Excel::extractExcelArray($file->getRealPath());
        $excelFields = array_values(array_flip(Config::get("custom.export.development")));
        $excelData = Excel::getData($list);

        $data = DevelopmentService::getInstance()->importDevelopment($excelData, $excelFields);
        return jsonResponse($data);
    }

    public function export(Request $request)
    {
        $development = $request->post("batch_data");
        $headerList = Config::get("custom.export.development");

        $excel = new Excel();
        $excel->setHeaderMap($headerList);
        $excel->setReverseMappings([
            'sex' => [
                1 => '男',
                2 => '女'
            ],
            "status" => [
                1 => "正常",
                2 => "禁用"
            ]
        ]);
        $result = $excel->export($development, "开发人员列表", ExcelConst::FORMAT_CSV);
        return $result;
    }
}